package com.onsproject.nzbgetremote;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockFragmentActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		ActionBar ab = getSupportActionBar();
		ab.addTab(ab.newTab().setText(R.string.basic).setTabListener(mActionBarTabListener));
		ab.addTab(ab.newTab().setText(R.string.logging).setTabListener(mActionBarTabListener));
		ab.addTab(ab.newTab().setText(R.string.status).setTabListener(mActionBarTabListener));
		// ab.addTab(ab.newTab().setText("Third").setTabListener(mActionBarTabListener));
		// ab.addTab(ab.newTab().setText("Fourth").setTabListener(mActionBarTabListener));

		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//		FragmentManager manager = getSupportFragmentManager();
//		FragmentTransaction fragmentTransaction = manager.beginTransaction();
//
//		fragmentTransaction.add(R.id.fragment_frame, new BasicCalls());
//		fragmentTransaction.commit();

		Settings.getInstance(MainActivity.this).checkBeforeDialog(MainActivity.this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.main_activity, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == R.id.submenu_1) {
			Settings.getInstance(MainActivity.this).showDialog(MainActivity.this);
		}
		return true;
	}

	private TabListener mActionBarTabListener = new TabListener() {

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {

			if (tab.getText().equals(getResources().getString(R.string.basic))) {
				ft.replace(R.id.fragment_frame, new BasicCalls());
			} else if (tab.getText().equals(getResources().getString(R.string.logging))) {
				ft.replace(R.id.fragment_frame, new LoggingFragment());
			} else if (tab.getText().equals(getResources().getString(R.string.status))) {
				ft.replace(R.id.fragment_frame, new StatusFragment());
			}
//			ft.commit();
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {

		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {

		}
	};
}
